<template><div><h2 id="助手类" tabindex="-1"><a class="header-anchor" href="#助手类"><span>助手类</span></a></h2>
<p>Yii 提供了许多类，可以帮助简化常见的编码任务，比如字符串或数组操作、HTML代码生成等。这些助手类被组织在 <code v-pre>yii\helpers</code> 命名空间下，并且都是静态类，这意味着它们只包含静态属性和方法，不应该被实例化。</p>
<p>你可以直接调用一个助手类的静态方法，如下所示:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>helpers<span class="token punctuation">\</span>Html</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">echo</span> <span class="token class-name static-context">Html</span><span class="token operator">::</span><span class="token function">encode</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Test > test'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>注意：为了支持自定义助手类，Yii 将每个核心助手类分解为两个类，一个基类（如<code v-pre>BaseArrayHelper</code>）和一个具体类（如 <code v-pre>ArrayHelper</code>）。当您使用助手时，应该只使用具体版本，而不要使用基类。</p>
</blockquote>
<h2 id="核心助手类" tabindex="-1"><a class="header-anchor" href="#核心助手类"><span>核心助手类</span></a></h2>
<p>Yii 版本中提供了以下核心助手类：</p>
<ul>
<li><a href="#bd9902">ArrayHelper</a>：数组</li>
<li><a href="#bccaa4">Console</a>：控制台</li>
<li><a href="#ca8ae6">FileHelper</a>：文件</li>
<li><a href="#f84611">FormatConverter</a> 格式转换器</li>
<li><a href="#3135f4">Html</a> HTML 标签</li>
<li><a href="#2342fd">HtmlPurifier</a> HTML 净化器</li>
<li><a href="#be53a0">Imagine (yii2-imagine 扩展提供)</a> 图片</li>
<li><a href="#3f515e">Inflector</a> 偏转器，英文名词单复数转化等</li>
<li><a href="#130ff9">IpHelper</a> IP 地址</li>
<li><a href="#eed8d8">Json</a> json</li>
<li><a href="#2182a7">Markdown</a> Markdown</li>
<li><a href="#36988d">StringHelper</a> 字符串</li>
<li><a href="#02a3a3">Url</a> URL</li>
<li><a href="#739ab5">VarDumper</a> 打印</li>
</ul>
<h2 id="arrayhelper" tabindex="-1"><a class="header-anchor" href="#arrayhelper"><span>ArrayHelper</span></a></h2>
<p>除了丰富的 <a href="https://www.php.net/manual/zh/book.array.php" target="_blank" rel="noopener noreferrer">PHP 数组函数集</a> 之外，Yii 的数组助手类，还提供了额外的静态方法，使您能够更有效地处理数组。</p>
<h3 id="arrayhelper-toarray" tabindex="-1"><a class="header-anchor" href="#arrayhelper-toarray"><span>ArrayHelper::toArray</span></a></h3>
<p>通常情况下，您可能需要将对象或对象数组转换为数组。最常见的情况，是转换活动记录模型，以便通过 REST API 来服务于数据数组，或者以其他方式使用它。下面的代码可以用来做这件事：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$posts</span> <span class="token operator">=</span> <span class="token class-name static-context">Post</span><span class="token operator">::</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">limit</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$data</span> <span class="token operator">=</span> <span class="token class-name static-context">ArrayHelper</span><span class="token operator">::</span><span class="token function">toArray</span><span class="token punctuation">(</span><span class="token variable">$posts</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'app\models\Post'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'id'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'title'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token comment">// the key name in array result => property name</span></span>
<span class="line">        <span class="token string single-quoted-string">'createTime'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'created_at'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token comment">// the key name in array result => anonymous function</span></span>
<span class="line">        <span class="token string single-quoted-string">'length'</span> <span class="token operator">=></span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$post</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token keyword">return</span> <span class="token function">strlen</span><span class="token punctuation">(</span><span class="token variable">$post</span><span class="token operator">-></span><span class="token property">content</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>第一个参数，包含我们想要转换的数据，在此例中，我们需要转换一个 <code v-pre>Post</code> AR 模型。<br>
第二个参数，是每个类的转换映射，在此例中，我们正在为 <code v-pre>Post</code> 模型设置一个映射，每个映射数组包含一组映射，每个映射可以是：</p>
<ul>
<li>要包含的字段名</li>
<li>所需的数组键名和要从中获取值的模型列名的键-值对。</li>
<li>所需数组键名的键值对和返回值的回调函数。</li>
</ul>
<p>以上对单个模型的换算结果为：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token number">123</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'test'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'createTime'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'2013-01-01 12:00AM'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'length'</span> <span class="token operator">=></span> <span class="token number">301</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>通过在类中实现 <code v-pre>yii\base\Arrayable</code> 接口，可以为特定类提供将对象转换为数组的默认方式。</p>
<h3 id="arrayhelper-merge" tabindex="-1"><a class="header-anchor" href="#arrayhelper-merge"><span>ArrayHelper::merge</span></a></h3>
<p>你可以使用 <code v-pre>yii\helpers\ArrayHelper::merge()</code> 来递归地合并两个或多个数组。如果每个数组都有一个具有相同字符串键值的元素，则后者将覆盖前者，这一点与 <a href="https://www.php.net/manual/zh/function.array-merge-recursive.php" target="_blank" rel="noopener noreferrer">PHP 数组函数 array_merge_recursive()</a> 不同。如果两个数组都有一个数组类型的元素并且具有相同的键值，则将进行递归合并。对于整型键元素，后一个数组中的元素将被追加到前一个数组中。</p>
<p>你可以使用 <code v-pre>yii\helpers\UnsetArrayValue</code> 对象来取消先前数组中的值，或者 <code v-pre>yii\helpers\ReplaceArrayValue</code> 来强制替换以前的值而不是递归合并。</p>
<p>例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$array1</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Yii'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'version'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'1.1'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'ids'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token number">1</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'validDomains'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'example.com'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'www.example.com'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'emails'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'admin'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'admin@example.com'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'dev'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'dev@example.com'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array2</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'version'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'2.0'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'ids'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token number">2</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'validDomains'</span> <span class="token operator">=></span> <span class="token keyword">new</span> <span class="token class-name class-name-fully-qualified"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>helpers<span class="token punctuation">\</span>ReplaceArrayValue</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'yiiframework.com'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'www.yiiframework.com'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'emails'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'dev'</span> <span class="token operator">=></span> <span class="token keyword">new</span> <span class="token class-name class-name-fully-qualified"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>helpers<span class="token punctuation">\</span>UnsetArrayValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">ArrayHelper</span><span class="token operator">::</span><span class="token function">merge</span><span class="token punctuation">(</span><span class="token variable">$array1</span><span class="token punctuation">,</span> <span class="token variable">$array2</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>结果为：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Yii'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'version'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'2.0'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'ids'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token number">1</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token number">2</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'validDomains'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'yiiframework.com'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'www.yiiframework.com'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'emails'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'admin'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'admin@example.com'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="arrayhelper-getvalue" tabindex="-1"><a class="header-anchor" href="#arrayhelper-getvalue"><span>ArrayHelper::getValue</span></a></h3>
<p>使用标准 PHP，从一个数组、对象或由这两者组成的复杂结构中检索值是非常重复性的。你必须先用 <code v-pre>isset</code> 来检查它的 key 是否存在，如果存在，你会得到它，如果不存在，提供默认值，具体如下：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">User</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token variable">$name</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'Alex'</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'foo'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'bar'</span> <span class="token operator">=></span> <span class="token keyword">new</span> <span class="token class-name">User</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token keyword">isset</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'bar'</span><span class="token punctuation">]</span><span class="token operator">-></span><span class="token property">name</span><span class="token punctuation">)</span> <span class="token operator">?</span> <span class="token variable">$array</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'bar'</span><span class="token punctuation">]</span><span class="token operator">-></span><span class="token property">name</span> <span class="token punctuation">:</span> <span class="token constant">null</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Yii 提供了一个非常方便的方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token class-name static-context">ArrayHelper</span><span class="token operator">::</span><span class="token function">getValue</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'foo.bar.name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>第一个方法参数是我们从哪里获取值。第二个参数指定如何获取数据。可能是下面的一种：</p>
<ul>
<li>要从中检索值的数组键或对象属性的名称。</li>
<li>以点分隔的数组键或对象属性名的集合。正是我们在上面的例子中使用的那个。</li>
<li>返回值的回调函数</li>
</ul>
<p>回调函数应该如下所示：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$fullName</span> <span class="token operator">=</span> <span class="token class-name static-context">ArrayHelper</span><span class="token operator">::</span><span class="token function">getValue</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">,</span> <span class="token variable">$defaultValue</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">firstName</span> <span class="token operator">.</span> <span class="token string single-quoted-string">' '</span> <span class="token operator">.</span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">lastName</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>第三个可选参数是默认值 <code v-pre>null</code>，如果没有指定，可以使用如下方式：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$username</span> <span class="token operator">=</span> <span class="token class-name static-context">ArrayHelper</span><span class="token operator">::</span><span class="token function">getValue</span><span class="token punctuation">(</span><span class="token variable">$comment</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'user.username'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Unknown'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="arrayhelper-setvalue" tabindex="-1"><a class="header-anchor" href="#arrayhelper-setvalue"><span>ArrayHelper::setValue</span></a></h3>
<p>从 Yii 2.0.13 版本开始，提供此方法，将值写入指定的键路径的关联数组。示例如下：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'key'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'in'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'k'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">]</span></span>
<span class="line">    <span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">ArrayHelper</span><span class="token operator">::</span><span class="token function">setValue</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'key.in'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'arr'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'val'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token comment">// the path to write the value in `$array` can be specified as an array</span></span>
<span class="line"><span class="token class-name static-context">ArrayHelper</span><span class="token operator">::</span><span class="token function">setValue</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'in'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'arr'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'val'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>结果，<code v-pre>$array['key']['in']</code> 的初始值将被新值覆盖：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'key'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'in'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'arr'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'val'</span><span class="token punctuation">]</span></span>
<span class="line">    <span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果路径包含一个不存在的键，它将被创建：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// if `$array['key']['in']['arr0']` is not empty, the value will be added to the array</span></span>
<span class="line"><span class="token class-name static-context">ArrayHelper</span><span class="token operator">::</span><span class="token function">setValue</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'key.in.arr0.arr1'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'val'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// if you want to completely override the value `$array['key']['in']['arr0']`</span></span>
<span class="line"><span class="token class-name static-context">ArrayHelper</span><span class="token operator">::</span><span class="token function">setValue</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'key.in.arr0'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'arr1'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'val'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>结果为：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'key'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'in'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'k'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'arr0'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'arr1'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'val'</span><span class="token punctuation">]</span></span>
<span class="line">        <span class="token punctuation">]</span></span>
<span class="line">    <span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="arrayhelper-remove" tabindex="-1"><a class="header-anchor" href="#arrayhelper-remove"><span>ArrayHelper::remove</span></a></h3>
<p>该方法可以从数组中移除项并返回该值。如果该键在数组中不存在，则返回默认值。如果你想要获取一个值，然后立即从数组中删除它，你可以使用 <code v-pre>remove</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'type'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'A'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'options'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$type</span> <span class="token operator">=</span> <span class="token class-name static-context">ArrayHelper</span><span class="token operator">::</span><span class="token function">remove</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'type'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><p>执行代码的 <code v-pre>$array</code> 将包含 <code v-pre>['options' =&gt; [1, 2]]</code>，<code v-pre>$type</code> 将为 <code v-pre>A</code>。注意，与 <code v-pre>getValue</code> 方法不同，<code v-pre>remove</code> 只支持简单的键名。</p>
<h3 id="arrayhelper-removevalue" tabindex="-1"><a class="header-anchor" href="#arrayhelper-removevalue"><span>ArrayHelper::removeValue</span></a></h3>
<p>从 Yii 2.0.11 版本开始提供这个方法，从数组中移除值匹配的项并返回被移除的项。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'Bob'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Dylan'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Michael'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Jackson'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Mick'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Jagger'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Janet'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Jackson'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$removed</span> <span class="token operator">=</span> <span class="token class-name static-context">ArrayHelper</span><span class="token operator">::</span><span class="token function">removeValue</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Jackson'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token comment">// result:</span></span>
<span class="line"><span class="token comment">// $array = ['Bob' => 'Dylan', 'Mick' => 'Jagger'];</span></span>
<span class="line"><span class="token comment">// $removed = ['Michael' => 'Jackson', 'Janet' => 'Jackson'];</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="arrayhelper-index" tabindex="-1"><a class="header-anchor" href="#arrayhelper-index"><span>ArrayHelper::index</span></a></h3>
<p>为了根据指定的键索引数组，可以使用 <code v-pre>index</code> 方法。输入应该是多维数组或对象数组。</p>
<p>第二个参数 <code v-pre>$key</code> 可以是子数组的键名，对象的属性名，或者一个必须返回值作为键的匿名函数。第三个参数 <code v-pre>$groups</code> 属性是一个键数组，用于根据指定的键将输入数组分组为一个或多个子数组。</p>
<p>如果 <code v-pre>$key</code> 属性或特定元素的值为 <code v-pre>null</code> 且 <code v-pre>$groups</code> 未定义，则数组元素将被丢弃。相反，如果指定了 <code v-pre>$groups</code>，数组元素将被添加到结果数组中，而不需要任何键。</p>
<p>例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'123'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'data'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'abc'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'device'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'laptop'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'345'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'data'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'def'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'device'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'tablet'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'345'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'data'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'hgi'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'device'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'smartphone'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">ArrayHelper</span><span class="token operator">::</span><span class="token function">index</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>结果将是一个关联数组，其中键是 <code v-pre>id</code> 属性的值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'123'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'123'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'data'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'abc'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'device'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'laptop'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'345'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'345'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'data'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'hgi'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'device'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'smartphone'</span><span class="token punctuation">]</span></span>
<span class="line">    <span class="token comment">// The second element of an original array is overwritten by the last element because of the same id</span></span>
<span class="line"><span class="token punctuation">]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>匿名函数，作为 <code v-pre>$key</code> 传递，给出相同的结果：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">ArrayHelper</span><span class="token operator">::</span><span class="token function">index</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$element</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$element</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>传递 <code v-pre>id</code> 作为第三个参数将 <code v-pre>$array</code> 通过 <code v-pre>id</code> 分组:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">ArrayHelper</span><span class="token operator">::</span><span class="token function">index</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token constant">null</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>按照 <code v-pre>id</code> 在第一级分组，而不是在第二级索引，结果将是一个多维数组:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'123'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'123'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'data'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'abc'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'device'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'laptop'</span><span class="token punctuation">]</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'345'</span> <span class="token operator">=></span> <span class="token punctuation">[</span> <span class="token comment">// all elements with this index are present in the result array</span></span>
<span class="line">        <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'345'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'data'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'def'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'device'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'tablet'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'345'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'data'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'hgi'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'device'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'smartphone'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>匿名函数也可以在分组数组中使用：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">ArrayHelper</span><span class="token operator">::</span><span class="token function">index</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'data'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$element</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$element</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'device'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在第一级按 <code v-pre>id</code> 分组，在第二级按 <code v-pre>device</code> 分组，在第三级按 <code v-pre>data</code> 索引，结果将是一个多维数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'123'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'laptop'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'abc'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'123'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'data'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'abc'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'device'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'laptop'</span><span class="token punctuation">]</span></span>
<span class="line">        <span class="token punctuation">]</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'345'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'tablet'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'def'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'345'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'data'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'def'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'device'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'tablet'</span><span class="token punctuation">]</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'smartphone'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'hgi'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'345'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'data'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'hgi'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'device'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'smartphone'</span><span class="token punctuation">]</span></span>
<span class="line">        <span class="token punctuation">]</span></span>
<span class="line">    <span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="arrayhelper-getcolumn" tabindex="-1"><a class="header-anchor" href="#arrayhelper-getcolumn"><span>ArrayHelper::getColumn</span></a></h3>
<p>该方法，返回数组中指定列的值。通常您可能需要从数据行或对象数组中获取一列值。常见的例子是获取一个 id 列表。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'123'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'data'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'abc'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'345'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'data'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'def'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$ids</span> <span class="token operator">=</span> <span class="token class-name static-context">ArrayHelper</span><span class="token operator">::</span><span class="token function">getColumn</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>结果为 <code v-pre>['123', '345']</code>。</p>
<p>如果需要额外的转换或者获取值的方式很复杂，第二个参数可以指定为匿名函数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">ArrayHelper</span><span class="token operator">::</span><span class="token function">getColumn</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$element</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$element</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="arrayhelper-map" tabindex="-1"><a class="header-anchor" href="#arrayhelper-map"><span>ArrayHelper::map</span></a></h3>
<p>为了从多维数组或对象数组中构建一个 map（键值对），你可以使用 <code v-pre>map</code> 方法。<code v-pre>$from</code> 和 <code v-pre>$to</code> 参数指定用于设置映射的键名或属性名。您也可以根据分组字段 <code v-pre>$group</code> 对映射进行分组。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'123'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'aaa'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'x'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'124'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'bbb'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'x'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'345'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'ccc'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'y'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">ArrayHelper</span><span class="token operator">::</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'id'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token comment">// the result is:</span></span>
<span class="line"><span class="token comment">// [</span></span>
<span class="line"><span class="token comment">//     '123' => 'aaa',</span></span>
<span class="line"><span class="token comment">//     '124' => 'bbb',</span></span>
<span class="line"><span class="token comment">//     '345' => 'ccc',</span></span>
<span class="line"><span class="token comment">// ]</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">ArrayHelper</span><span class="token operator">::</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'id'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'class'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token comment">// the result is:</span></span>
<span class="line"><span class="token comment">// [</span></span>
<span class="line"><span class="token comment">//     'x' => [</span></span>
<span class="line"><span class="token comment">//         '123' => 'aaa',</span></span>
<span class="line"><span class="token comment">//         '124' => 'bbb',</span></span>
<span class="line"><span class="token comment">//     ],</span></span>
<span class="line"><span class="token comment">//     'y' => [</span></span>
<span class="line"><span class="token comment">//         '345' => 'ccc',</span></span>
<span class="line"><span class="token comment">//     ],</span></span>
<span class="line"><span class="token comment">// ]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="arrayhelper-keyexists" tabindex="-1"><a class="header-anchor" href="#arrayhelper-keyexists"><span>ArrayHelper::keyExists</span></a></h3>
<p><code v-pre>ArrayHelper::keyExists</code> 的工作方式与 <a href="https://www.php.net/manual/zh/function.array-key-exists.php" target="_blank" rel="noopener noreferrer">PHP 数组函数 array_key_exists()</a> 类似，额外的，它还支持不区分大小写的键比较。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$data1</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'userName'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Alex'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$data2</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'username'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Carsten'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token class-name static-context">ArrayHelper</span><span class="token operator">::</span><span class="token function">keyExists</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'username'</span><span class="token punctuation">,</span> <span class="token variable">$data1</span><span class="token punctuation">,</span> <span class="token constant boolean">false</span><span class="token punctuation">)</span> <span class="token operator">||</span> <span class="token operator">!</span><span class="token class-name static-context">ArrayHelper</span><span class="token operator">::</span><span class="token function">keyExists</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'username'</span><span class="token punctuation">,</span> <span class="token variable">$data2</span><span class="token punctuation">,</span> <span class="token constant boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">echo</span> <span class="token string double-quoted-string">"Please provide username."</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="arrayhelper-multisort" tabindex="-1"><a class="header-anchor" href="#arrayhelper-multisort"><span>ArrayHelper::multisort</span></a></h3>
<p><code v-pre>multisort</code> 方法，有助于我们按一个或几个键，对对象数组或嵌套数组进行排序。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$data</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'age'</span> <span class="token operator">=></span> <span class="token number">30</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Alexander'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'age'</span> <span class="token operator">=></span> <span class="token number">30</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Brian'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'age'</span> <span class="token operator">=></span> <span class="token number">19</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Barney'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token class-name static-context">ArrayHelper</span><span class="token operator">::</span><span class="token function">multisort</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'age'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token constant">SORT_ASC</span><span class="token punctuation">,</span> <span class="token constant">SORT_DESC</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在排序之后，我们将在 <code v-pre>$data</code> 中得到以下内容：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">[</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'age'</span> <span class="token operator">=></span> <span class="token number">19</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Barney'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'age'</span> <span class="token operator">=></span> <span class="token number">30</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Brian'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'age'</span> <span class="token operator">=></span> <span class="token number">30</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Alexander'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>第二个参数，用来指定要排序的键，如果是一个单键，可以是一个字符串，如果是多个键，或者一个匿名函数，可以是一个数组，就像下面的一个：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">ArrayHelper</span><span class="token operator">::</span><span class="token function">multisort</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token variable">$item</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// sort by age if it exists or by name otherwise</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token keyword">isset</span><span class="token punctuation">(</span><span class="token variable">$item</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'age'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">?</span> <span class="token variable">$item</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'age'</span><span class="token punctuation">]</span> <span class="token punctuation">:</span> <span class="token variable">$item</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>第三个参数是排序方向。如果按单个键排序，它可以是 <code v-pre>SORT_ASC</code> 或 <code v-pre>SORT_DESC</code>。如果按多个值排序，则可以通过提供排序方向数组对每个值进行不同的排序。</p>
<p>最后一个参数是 PHP 排序标志，它可以接受与传递给 <a href="https://www.php.net/manual/zh/function.sort.php" target="_blank" rel="noopener noreferrer">PHP 函数 sort()</a> 的值相同的值。</p>
<h3 id="arrayhelper-htmlencode" tabindex="-1"><a class="header-anchor" href="#arrayhelper-htmlencode"><span>ArrayHelper::htmlEncode</span></a></h3>
<p>该方法，将字符串数组中的特殊字符编码成HTML实体。具体如下：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$encoded</span> <span class="token operator">=</span> <span class="token class-name static-context">ArrayHelper</span><span class="token operator">::</span><span class="token function">htmlEncode</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>默认情况下，只对数组值进行编码。如果一个值是一个数组，这个方法也会递归地对它进行编码。只对字符串值进行编码。</p>
<p>通过传递第二个参数为 <code v-pre>false</code>，你也可以编码数组的键。编码将使用应用程序字符集，可以通过第三个参数进行更改。</p>
<h3 id="arrayhelper-htmldecode" tabindex="-1"><a class="header-anchor" href="#arrayhelper-htmldecode"><span>ArrayHelper::htmlDecode</span></a></h3>
<p>该方法，将 HTML 实体解码为字符串数组中相应的字符。具体如下：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$decoded</span> <span class="token operator">=</span> <span class="token class-name static-context">ArrayHelper</span><span class="token operator">::</span><span class="token function">htmlDecode</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>默认情况下，只有数组值将被解码。如果一个值是一个数组，这个方法也会递归地解码它。只有字符串值将被解码。</p>
<p>通过传递第二个参数为 <code v-pre>false</code>，你也可以编码数组的键。编码将使用应用程序字符集，可以通过第三个参数进行更改。</p>
<h3 id="arrayhelper-isassociative" tabindex="-1"><a class="header-anchor" href="#arrayhelper-isassociative"><span>ArrayHelper::isAssociative</span></a></h3>
<p>该方法，返回一个值，该值指示给定数组是否是关联数组。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// all keys are strings</span></span>
<span class="line"><span class="token variable">$associative</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'framework'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Yii'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'version'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'2.0'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">echo</span> <span class="token class-name static-context">ArrayHelper</span><span class="token operator">::</span><span class="token function">isAssociative</span><span class="token punctuation">(</span><span class="token variable">$associative</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果数组的所有键都是字符串，则数组是关联的。如果第二个参数 <code v-pre>$allStrings</code> 值是 <code v-pre>false</code>, 然后，如果数组中至少有一个键是字符串，则该数组将被视为关联数组。</p>
<blockquote>
<p>注意： 空数组不会被认为是一个关联数组的。</p>
</blockquote>
<h3 id="arrayhelper-isindexed" tabindex="-1"><a class="header-anchor" href="#arrayhelper-isindexed"><span>ArrayHelper::isIndexed</span></a></h3>
<p>该方法，返回一个值，该值指示给定数组是否是索引数组。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// no keys specified</span></span>
<span class="line"><span class="token variable">$indexed</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'Qiang'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Paul'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">echo</span> <span class="token class-name static-context">ArrayHelper</span><span class="token operator">::</span><span class="token function">isIndexed</span><span class="token punctuation">(</span><span class="token variable">$indexed</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果数组的所有键都是整数，数组就会被索引。如果第二个参数 <code v-pre>$consecutive</code> 的值是 <code v-pre>true</code>，然后，数组键必须是从 0 开始的连续序列。</p>
<blockquote>
<p>注意：空数组将被视为一个索引数组。</p>
</blockquote>
<h3 id="arrayhelper-isin" tabindex="-1"><a class="header-anchor" href="#arrayhelper-isin"><span>ArrayHelper::isIn</span></a></h3>
<p>Yii 2.0.7 版本开始提供该方法，用来检查数组或 实现 <code v-pre>Traversable</code> 接口的对象，是否包含元素。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// true</span></span>
<span class="line"><span class="token class-name static-context">ArrayHelper</span><span class="token operator">::</span><span class="token function">isIn</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'a'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'a'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"><span class="token class-name static-context">ArrayHelper</span><span class="token operator">::</span><span class="token function">isIn</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'a'</span><span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">ArrayObject</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'a'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>该方法与 PHP 函数 <a href="https://www.php.net/manual/zh/function.in-array.php" target="_blank" rel="noopener noreferrer">in_array()</a> 作用类似，但是，额外还适用于实现 <code v-pre>Traversable</code> 接口的对象。</p>
<h3 id="arrayhelper-istraversable" tabindex="-1"><a class="header-anchor" href="#arrayhelper-istraversable"><span>ArrayHelper::isTraversable</span></a></h3>
<p>Yii 2.0.8 版本开始提供该方法，用来检查一个变量是数组还是 <code v-pre>Traversable</code>。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// true</span></span>
<span class="line"><span class="token class-name static-context">ArrayHelper</span><span class="token operator">::</span><span class="token function">isTraversable</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'a'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"><span class="token class-name static-context">ArrayHelper</span><span class="token operator">::</span><span class="token function">isTraversable</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">ArrayObject</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'a'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>该方法与 PHP 函数 <a href="https://www.php.net/manual/en/function.is-array.php" target="_blank" rel="noopener noreferrer">is_array()</a> 作用类似，但是，额外还适用于实现 <code v-pre>Traversable</code> 接口的对象。</p>
<h3 id="arrayhelper-issubset" tabindex="-1"><a class="header-anchor" href="#arrayhelper-issubset"><span>ArrayHelper::isSubset</span></a></h3>
<p>Yii 2.0.7 版本开始提供该方法，用来检查一个数组或 <code v-pre>Traversable</code> 是否是另一个数组或 <code v-pre>Traversable</code> 的子集。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// true</span></span>
<span class="line"><span class="token class-name static-context">ArrayHelper</span><span class="token operator">::</span><span class="token function">isSubset</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'a'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'c'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'a'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'b'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'c'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"><span class="token class-name static-context">ArrayHelper</span><span class="token operator">::</span><span class="token function">isSubset</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">ArrayObject</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'a'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'c'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">ArrayObject</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'a'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'b'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'c'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果首参 <code v-pre>$needles</code> 的所有元素都包含在第二个参数 <code v-pre>$haystack</code> 中，这个方法将返回 <code v-pre>true</code>。如果至少缺少一个元素，则返回 <code v-pre>false</code>。</p>
<h3 id="arrayhelper-filter" tabindex="-1"><a class="header-anchor" href="#arrayhelper-filter"><span>ArrayHelper::filter</span></a></h3>
<p>Yii 2.0.9 版本开始提供该方法，用来根据指定的规则过滤数组。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'A'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'B'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'C'</span> <span class="token operator">=></span> <span class="token number">1</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'D'</span> <span class="token operator">=></span> <span class="token number">2</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'E'</span> <span class="token operator">=></span> <span class="token number">1</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>helpers<span class="token punctuation">\</span>ArrayHelper</span><span class="token operator">::</span><span class="token function">filter</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'A'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token comment">// $result will be:</span></span>
<span class="line"><span class="token comment">// [</span></span>
<span class="line"><span class="token comment">//     'A' => [1, 2],</span></span>
<span class="line"><span class="token comment">// ]</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>helpers<span class="token punctuation">\</span>ArrayHelper</span><span class="token operator">::</span><span class="token function">filter</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'A'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'B.C'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token comment">// $result will be:</span></span>
<span class="line"><span class="token comment">// [</span></span>
<span class="line"><span class="token comment">//     'A' => [1, 2],</span></span>
<span class="line"><span class="token comment">//     'B' => ['C' => 1],</span></span>
<span class="line"><span class="token comment">// ]</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>helpers<span class="token punctuation">\</span>ArrayHelper</span><span class="token operator">::</span><span class="token function">filter</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'B'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'!B.C'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token comment">// $result will be:</span></span>
<span class="line"><span class="token comment">// [</span></span>
<span class="line"><span class="token comment">//     'B' => ['D' => 2],</span></span>
<span class="line"><span class="token comment">// ]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>第二个参数 <code v-pre>$filters</code>，定义数组键的规则，这些键应该从结果中保留或删除。每个规则是：</p>
<ul>
<li><code v-pre>var</code>：<code v-pre>$array['var']</code> 将留在结果中</li>
<li><code v-pre>var.key</code>：只有 <code v-pre>$array['var']['key']</code> 将留在结果中</li>
<li><code v-pre>!var.key</code>：<code v-pre>$array['var']['key']</code> 将会从结果中移除</li>
</ul>
<h2 id="console" tabindex="-1"><a class="header-anchor" href="#console"><span>Console</span></a></h2>
<p>控制台助手类，为与命令行相关的任务提供了有用的方法，比如，获取输入、格式化和彩色输出等。一般在继承了 <code v-pre>yii\console\Controller</code> 类的控制器中的动作方法中使用，例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">namespace</span> <span class="token package">console<span class="token punctuation">\</span>controllers</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>console<span class="token punctuation">\</span>Controller</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>helpers<span class="token punctuation">\</span>Console</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">TestController</span> <span class="token keyword">extends</span> <span class="token class-name">Controller</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">actionIndex</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token comment">// Console:: ...</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// 输出</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">stdout</span><span class="token punctuation">(</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token function">ansiFormat</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Success'</span><span class="token punctuation">,</span> <span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token constant">FG_GREEN</span><span class="token punctuation">)</span> <span class="token operator">.</span> <span class="token string double-quoted-string">"\n"</span><span class="token punctuation">,</span> <span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token constant">ITALIC</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="console-movecursorup" tabindex="-1"><a class="header-anchor" href="#console-movecursorup"><span>Console::moveCursorUp</span></a></h3>
<p>通过向终端发送 ANSI 控制代码 CUU，向上移动终端光标。如果光标已经在屏幕边缘，则没有效果。示例如下：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// $rows 光标应该向上移动的行数，默认为 1</span></span>
<span class="line"><span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">moveCursorUp</span><span class="token punctuation">(</span><span class="token variable">$rows</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="console-movecursordown" tabindex="-1"><a class="header-anchor" href="#console-movecursordown"><span>Console::moveCursorDown</span></a></h3>
<p>通过向终端发送 ANSI 控制代码 CUD，向下移动终端光标。如果光标已经在屏幕边缘，则没有效果。示例如下：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// $rows 光标应该向下移动的行数，默认为 1</span></span>
<span class="line"><span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">moveCursorDown</span><span class="token punctuation">(</span><span class="token variable">$rows</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="console-movecursorforward" tabindex="-1"><a class="header-anchor" href="#console-movecursorforward"><span>Console::moveCursorForward</span></a></h3>
<p>通过向终端发送 ANSI 控制代码 CUF，向前移动终端光标。如果光标已经在屏幕边缘，则没有效果。示例如下：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// $steps 光标向前移动的步数，默认为 1</span></span>
<span class="line"><span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">moveCursorForward</span><span class="token punctuation">(</span><span class="token variable">$steps</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="console-movecursorbackward" tabindex="-1"><a class="header-anchor" href="#console-movecursorbackward"><span>Console::moveCursorBackward</span></a></h3>
<p>通过向终端发送 ANSI 控制代码 CUB，向后移动终端光标。如果光标已经在屏幕边缘，则没有效果。示例如下：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// $steps 光标向后移动的步数，默认为 1</span></span>
<span class="line"><span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">moveCursorBackward</span><span class="token punctuation">(</span><span class="token variable">$steps</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="console-movecursornextline" tabindex="-1"><a class="header-anchor" href="#console-movecursornextline"><span>Console::moveCursorNextLine</span></a></h3>
<p>通过向终端发送 ANSI 控制代码 CNL，将终端光标移动到下一行的开头。示例如下：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// $lines 光标应该向下移动的行数，默认为 1</span></span>
<span class="line"><span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">moveCursorNextLine</span><span class="token punctuation">(</span><span class="token variable">$lines</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="console-movecursorprevline" tabindex="-1"><a class="header-anchor" href="#console-movecursorprevline"><span>Console::moveCursorPrevLine</span></a></h3>
<p>通过向终端发送 ANSI 控制代码 CPL，将终端光标移动到上一行的开头。示例如下：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// $lines 光标应该向上移动的行数，默认为 1</span></span>
<span class="line"><span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">moveCursorPrevLine</span><span class="token punctuation">(</span><span class="token variable">$lines</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="console-movecursorto" tabindex="-1"><a class="header-anchor" href="#console-movecursorto"><span>Console::moveCursorTo</span></a></h3>
<p>通过向终端发送 ANSI 控制代码 CUP 或 CHA，将光标移动到作为列和行给定的绝对位置。示例如下：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// $column 基于1的列号，1是屏幕的左边缘。</span></span>
<span class="line"><span class="token comment">// $row 基于1的行号，1是屏幕的上边缘。如果没有设置，将只移动光标在当前行。</span></span>
<span class="line"><span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">moveCursorTo</span><span class="token punctuation">(</span><span class="token variable">$column</span><span class="token punctuation">,</span> <span class="token variable">$row</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="console-scrollup" tabindex="-1"><a class="header-anchor" href="#console-scrollup"><span>Console::scrollUp</span></a></h3>
<p>通过向终端发送 ANSI 控制代码 SU 来向上滚动整个页面。新行会添加在底部。在 windows 中使用 ANSI.SYS 是不支持的。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// $lines 向上滚动的行数，默认为 1</span></span>
<span class="line"><span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">scrollUp</span><span class="token punctuation">(</span><span class="token variable">$lines</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="console-scrolldown" tabindex="-1"><a class="header-anchor" href="#console-scrolldown"><span>Console::scrollDown</span></a></h3>
<p>通过向终端发送 ANSI 控制代码 SU 来向下滚动整个页面。新行会添加在顶部。在 windows 中使用 ANSI.SYS 是不支持的。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// $lines 向下滚动的行数，默认为 1</span></span>
<span class="line"><span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">scrollDown</span><span class="token punctuation">(</span><span class="token variable">$lines</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="console-savecursorposition" tabindex="-1"><a class="header-anchor" href="#console-savecursorposition"><span>Console::saveCursorPosition</span></a></h3>
<p>通过向终端发送 ANSI 控制代码 SCP 来保存当前光标位置。然后可以使用 <code v-pre>restoreCursorPosition()</code> 恢复位置。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">saveCursorPosition</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="console-restorecursorposition" tabindex="-1"><a class="header-anchor" href="#console-restorecursorposition"><span>Console::restoreCursorPosition</span></a></h3>
<p>通过发送 ANSI 控制代码 RCP 到终端，恢复用 <code v-pre>saveCursorPosition()</code> 保存的光标位置。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">restoreCursorPosition</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="console-hidecursor" tabindex="-1"><a class="header-anchor" href="#console-hidecursor"><span>Console::hideCursor</span></a></h3>
<p>通过向终端发送 ANSI DECTCEM 码 ?25l 来隐藏游标。使用 <code v-pre>showCursor()</code> 恢复显示。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">hideCursor</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><blockquote>
<p>注意：不要忘记在应用程序退出时显示光标。退出后光标可能会隐藏在终端中。</p>
</blockquote>
<h3 id="console-showcursor" tabindex="-1"><a class="header-anchor" href="#console-showcursor"><span>Console::showCursor</span></a></h3>
<p>当 <code v-pre>hideCursor()</code> 通过向终端发送 ANSI DECTCEM 码 ?25h 来隐藏光标时，将再次显示光标。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">showCursor</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="console-clearscreen" tabindex="-1"><a class="header-anchor" href="#console-clearscreen"><span>Console::clearScreen</span></a></h3>
<p>通过向终端发送参数 2 的 ANSI 控制代码 ED 来清除整个屏幕内容。光标的位置不会被改变。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">clearScreen</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><blockquote>
<p>注意：在 windows 中使用 ANSI.SYS 实现，将光标位置重置到屏幕的左上角。</p>
</blockquote>
<h3 id="console-clearscreenbeforecursor" tabindex="-1"><a class="header-anchor" href="#console-clearscreenbeforecursor"><span>Console::clearScreenBeforeCursor</span></a></h3>
<p>通过向终端发送参数 1 的 ANSI 控制代码 ED，清除从光标到屏幕开头的文本。光标的位置不会被改变。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">clearScreenBeforeCursor</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="console-clearscreenaftercursor" tabindex="-1"><a class="header-anchor" href="#console-clearscreenaftercursor"><span>Console::clearScreenAfterCursor</span></a></h3>
<p>通过向终端发送参数为 0 的 ANSI 控制代码 ED，清除从光标到屏幕末端的文本。光标的位置不会被改变。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">clearScreenAfterCursor</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="console-clearline" tabindex="-1"><a class="header-anchor" href="#console-clearline"><span>Console::clearLine</span></a></h3>
<p>通过将参数 2 的 ANSI 控制代码 EL 发送给终端，清除当前光标所在的行。光标的位置不会被改变。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">clearLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="console-clearlinebeforecursor" tabindex="-1"><a class="header-anchor" href="#console-clearlinebeforecursor"><span>Console::clearLineBeforeCursor</span></a></h3>
<p>通过向终端发送参数为 1 的 ANSI 控制代码 EL，清除从光标位置到行首的文本。光标的位置不会被改变。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">clearLineBeforeCursor</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="console-clearlineaftercursor" tabindex="-1"><a class="header-anchor" href="#console-clearlineaftercursor"><span>Console::clearLineAfterCursor</span></a></h3>
<p>通过向终端发送参数为 0 的 ANSI 控制代码 EL，清除从光标位置到行尾的文本。光标的位置不会被改变。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">clearLineAfterCursor</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="console-ansiformatcode" tabindex="-1"><a class="header-anchor" href="#console-ansiformatcode"><span>Console::ansiFormatCode</span></a></h3>
<p>根据给定的格式化常量，返回 ANSI 格式代码。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// $format 包含格式化值的数组</span></span>
<span class="line"><span class="token comment">// 你可以传递任何 `FG_*`，`BG_*` 等格式的常量</span></span>
<span class="line"><span class="token comment">// 或者 [[xtermFgColor]] 和 [[xtermBgColor]] 指定格式。</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$format</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token constant">FG_RED</span><span class="token punctuation">,</span> <span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token constant">BG_GREEN</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$ansi_code</span> <span class="token operator">=</span> <span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">ansiFormatCode</span><span class="token punctuation">(</span><span class="token variable">$format</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="console-beginansiformat" tabindex="-1"><a class="header-anchor" href="#console-beginansiformat"><span>Console::beginAnsiFormat</span></a></h3>
<p>输出 ANSI 格式代码，该代码会影响随后打印的任何文本的格式。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">beginAnsiFormat</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token constant">FG_RED</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="console-endansiformat" tabindex="-1"><a class="header-anchor" href="#console-endansiformat"><span>Console::endAnsiFormat</span></a></h3>
<p>重置之前 <code v-pre>beginAnsiFormat()</code> 方法设置的任何 ANSI 格式，在此之后的任何输出将具有默认的文本格式。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">endAnsiFormat</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token comment">// 等价于</span></span>
<span class="line"><span class="token keyword">echo</span> <span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">ansiFormatCode</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token constant">RESET</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="console-ansiformat" tabindex="-1"><a class="header-anchor" href="#console-ansiformat"><span>Console::ansiFormat</span></a></h3>
<p>将返回一个以给定 ANSI 样式格式化的字符串。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$str_ansi</span> <span class="token operator">=</span> <span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">ansiFormat</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'xxx'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token constant">FG_RED</span><span class="token punctuation">,</span> <span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token constant">BG_GREEN</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="console-xtermfgcolor" tabindex="-1"><a class="header-anchor" href="#console-xtermfgcolor"><span>Console::xtermFgColor</span></a></h3>
<p>返回 xterm 前景色的 ANSI 格式代码。你可以将这个返回值传递给 <code v-pre>ansiFormat</code>、<code v-pre>ansiFormatCode</code> 和 <code v-pre>beginAnsiFormat</code> 等其中一个格式化方法。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$code_ansi</span> <span class="token operator">=</span> <span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">xtermFgColor</span><span class="token punctuation">(</span><span class="token variable">$xterm_color_code</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="console-xtermbgcolor" tabindex="-1"><a class="header-anchor" href="#console-xtermbgcolor"><span>Console::xtermBgColor</span></a></h3>
<p>返回 xterm 背景颜色的 ANSI 格式代码。你可以将这个返回值传递给 <code v-pre>ansiFormat</code>、<code v-pre>ansiFormatCode</code> 和 <code v-pre>beginAnsiFormat</code> 等其中一个格式化方法。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$code_ansi</span> <span class="token operator">=</span> <span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">xtermBgColor</span><span class="token punctuation">(</span><span class="token variable">$xterm_color_code</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="console-stripansiformat" tabindex="-1"><a class="header-anchor" href="#console-stripansiformat"><span>Console::stripAnsiFormat</span></a></h3>
<p>从字符串中剥离 ANSI 控制代码。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$str</span> <span class="token operator">=</span> <span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">stripAnsiFormat</span><span class="token punctuation">(</span><span class="token variable">$str_has_ansi_control_code</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="console-ansistrlen" tabindex="-1"><a class="header-anchor" href="#console-ansistrlen"><span>Console::ansiStrlen</span></a></h3>
<p>返回不带 ANSI 颜色代码的字符串长度。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$length</span> <span class="token operator">=</span> <span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">ansiStrlen</span><span class="token punctuation">(</span><span class="token variable">$str_has_ansi_corlor_code</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="console-ansistrwidth" tabindex="-1"><a class="header-anchor" href="#console-ansistrwidth"><span>Console::ansiStrwidth</span></a></h3>
<p>Yii 2.0.36 版本开始提供此方法，返回不带 ANSI 颜色代码的字符串的宽度。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$width</span> <span class="token operator">=</span> <span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">ansiStrwidth</span><span class="token punctuation">(</span><span class="token variable">$str_has_ansi_corlor_code</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="console-ansicolorizedsubstr" tabindex="-1"><a class="header-anchor" href="#console-ansicolorizedsubstr"><span>Console::ansiColorizedSubstr</span></a></h3>
<p>返回由开始和长度参数指定的字符串的 ANSI 颜色代码部分。如果字符串有颜色代码，那么将返回 “TEXT_COLOR + TEXT_STRING + DEFAULT_COLOR”，否则将是简单的 “TEXT_STRING”。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$str_ansi_color</span> <span class="token operator">=</span> <span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">ansiColorizedSubstr</span><span class="token punctuation">(</span><span class="token variable">$str</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="console-ansitohtml" tabindex="-1"><a class="header-anchor" href="#console-ansitohtml"><span>Console::ansiToHtml</span></a></h3>
<p>将 ANSI 格式的字符串转换为 HTML。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// $styleMap 一个可选的 ANSI 控制代码的映射，例如，FG\_*COLOR* 或 [[BOLD]] 指向一组 css 样式定义，CSS 样式定义表示为一个数组，数组键对应于 CSS 样式属性名，值是 CSS 值。值可以是数组，在呈现时使用 `' '` 进行合并和内爆。</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$styleMap</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token constant">FG_RED</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'color'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'red'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$html</span> <span class="token operator">=</span> <span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">ansiToHtml</span><span class="token punctuation">(</span><span class="token variable">$str_ansi</span><span class="token punctuation">,</span> <span class="token variable">$styleMap</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>注意：xTerm 256 位颜色目前不支持。</p>
</blockquote>
<h3 id="console-markdowntoansi" tabindex="-1"><a class="header-anchor" href="#console-markdowntoansi"><span>Console::markdownToAnsi</span></a></h3>
<p>通过应用一些 ANSI 格式将 Markdown 转换为在控制台环境中更好的可读性。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$str</span> <span class="token operator">=</span> <span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">ansiToHtml</span><span class="token punctuation">(</span><span class="token variable">$str_markdown</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="console-rendercoloredstring" tabindex="-1"><a class="header-anchor" href="#console-rendercoloredstring"><span>Console::renderColoredString</span></a></h3>
<p>将字符串转换为 ANSI 格式，方法是用 ANSI 控制代码替换像 %y（表示黄色）这样的模式。与 <a href="https://github.com/pear/Console_Color2/blob/master/Console/Color2.php" target="_blank" rel="noopener noreferrer">github.com/pear/Console_Color2/blo...</a> 使用几乎相同的语法。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// $str 参数是要转换的字符串</span></span>
<span class="line"><span class="token comment">// $colored 标志是否使用颜色，默认为 true，如果设置为 false，颜色代码将被删除（%% 将被转换为 %）</span></span>
<span class="line"><span class="token variable">$str_ansi</span> <span class="token operator">=</span> <span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">renderColoredString</span><span class="token punctuation">(</span><span class="token variable">$str</span><span class="token punctuation">,</span> <span class="token variable">$colored</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>转换表是：(在某些终端上 ‘粗体’ 表示 ‘轻’)。它几乎与 irssi 使用的转换表相同。例如：</p>
<pre><code>             text      text            background
 ------------------------------------------------
 %k %K %0    black     dark grey       black
 %r %R %1    red       bold red        red
 %g %G %2    green     bold green      green
 %y %Y %3    yellow    bold yellow     yellow
 %b %B %4    blue      bold blue       blue
 %m %M %5    magenta   bold magenta    magenta
 %p %P       magenta (think: purple)
 %c %C %6    cyan      bold cyan       cyan
 %w %W %7    white     bold white      white

 %F     Blinking, Flashing
 %U     Underline
 %8     Reverse
 %\_,%9  Bold

 %n     Resets the color
 %%     A single %
</code></pre>
<h3 id="console-escape" tabindex="-1"><a class="header-anchor" href="#console-escape"><span>Console::escape</span></a></h3>
<p>转义 %，所以，当 <code v-pre>renderColoredString</code> 解析字符串时，它们不会被解释为颜色代码。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$str_escape</span> <span class="token operator">=</span> <span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">escape</span><span class="token punctuation">(</span><span class="token variable">$str</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="console-streamsupportsansicolors" tabindex="-1"><a class="header-anchor" href="#console-streamsupportsansicolors"><span>Console::streamSupportsAnsiColors</span></a></h3>
<p>如果 stream 支持着色，则返回 true，如果 stream 不支持 ANSI 颜色，那么它不可用。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$is_support</span> <span class="token operator">=</span> <span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">streamSupportsAnsiColors</span><span class="token punctuation">(</span><span class="token variable">$stream</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="console-isrunningonwindows" tabindex="-1"><a class="header-anchor" href="#console-isrunningonwindows"><span>Console::isRunningOnWindows</span></a></h3>
<p>如果控制台在 windows 上运行，则返回 true，反之 false。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$is_windows</span> <span class="token operator">=</span> <span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">streamSupportsAnsiColors</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="console-getscreensize" tabindex="-1"><a class="header-anchor" href="#console-getscreensize"><span>Console::getScreenSize</span></a></h3>
<p>返回终端屏幕尺寸。当它不能确定大小的时候，会返回 false。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// $refresh 是否强制检查和不重用缓存大小值，默认为 false</span></span>
<span class="line"><span class="token keyword">list</span><span class="token punctuation">(</span><span class="token variable">$width</span><span class="token punctuation">,</span> <span class="token variable">$height</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token class-name static-context">ConsoleHelper</span><span class="token operator">::</span><span class="token function">getScreenSize</span><span class="token punctuation">(</span><span class="token variable">$refresh</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>注意：这个方法对于在应用程序运行时，检测窗口大小的变化非常有用，但可能无法在每个终端上获得最新的值。</p>
</blockquote>
<h3 id="console-wraptext" tabindex="-1"><a class="header-anchor" href="#console-wraptext"><span>Console::wrapText</span></a></h3>
<p>Yii 2.0.4 版本开始提供该方法，返回带缩进以适应屏幕大小的换行文本。如果不能检测到屏幕大小，或者缩进大于屏幕大小，文本将不会被换行。第一行将 <strong>不</strong> 缩进。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// $indent 用于缩进的空格数，默认为 0</span></span>
<span class="line"><span class="token comment">// $refresh 是否强制刷新屏幕大小，默认为 false</span></span>
<span class="line"><span class="token variable">$text</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'Lorem ipsum dolor sit amet.'</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$indent</span> <span class="token operator">=</span> <span class="token number">4</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$text_wrapped</span> <span class="token operator">=</span> <span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">wrapText</span><span class="token punctuation">(</span><span class="token variable">$text</span><span class="token punctuation">,</span> <span class="token variable">$indent</span><span class="token punctuation">,</span> <span class="token variable">$refresh</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 假设指定屏幕宽度为 16 个字符，结果是：</span></span>
<span class="line">Lorem ipsum</span>
<span class="line">    dolor sit</span>
<span class="line">    amet<span class="token operator">.</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="console-stdin" tabindex="-1"><a class="header-anchor" href="#console-stdin"><span>Console::stdin</span></a></h3>
<p>从 STDIN 获取输入，并返回针对换行符 EOL 进行从右边裁剪（<a href="https://php.net/manual/zh/function.rtrim.php" title="PHP 函数 rtrim()" target="_blank" rel="noopener noreferrer">PHP 函数 rtrim()</a>）的字符串。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// $raw 如果设置为true，则返回原始字符串而不进行修剪，默认为 false</span></span>
<span class="line"><span class="token variable">$str</span> <span class="token operator">=</span> <span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">stdin</span><span class="token punctuation">(</span><span class="token variable">$raw</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="console-stdout" tabindex="-1"><a class="header-anchor" href="#console-stdout"><span>Console::stdout</span></a></h3>
<p>打印一个字符串到 STDOUT，成功时返回打印的字节数，错误时为 false，例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$num_bytes</span> <span class="token operator">=</span> <span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">stdout</span><span class="token punctuation">(</span><span class="token variable">$str</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="console-stderr" tabindex="-1"><a class="header-anchor" href="#console-stderr"><span>Console::stderr</span></a></h3>
<p>打印一个字符串到 STDERR，成功时返回打印的字节数，错误时为 false，例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$num_bytes</span> <span class="token operator">=</span> <span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">stderr</span><span class="token punctuation">(</span><span class="token variable">$str</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="console-input" tabindex="-1"><a class="header-anchor" href="#console-input"><span>Console::input</span></a></h3>
<p>请求用户输入，当用户输入回车（PHP_EOL）时结束。它还可以提供一个提示符。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// $prompt 等待输入之前要显示的提示符，默认是 null</span></span>
<span class="line"><span class="token variable">$str</span> <span class="token operator">=</span> <span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">input</span><span class="token punctuation">(</span><span class="token variable">$prompt</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="console-output" tabindex="-1"><a class="header-anchor" href="#console-output"><span>Console::output</span></a></h3>
<p>将文本打印到 STDOUT，并附加回车符（PHP_EOL）。成功时返回打印的字节数，错误时为 false，例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$num_bytes</span> <span class="token operator">=</span> <span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">output</span><span class="token punctuation">(</span><span class="token variable">$str</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="console-error" tabindex="-1"><a class="header-anchor" href="#console-error"><span>Console::error</span></a></h3>
<p>打印文本到 STDERR，并附加一个回车符（PHP_EOL）。成功时返回打印的字节数，错误时为 false，例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$num_bytes</span> <span class="token operator">=</span> <span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">error</span><span class="token punctuation">(</span><span class="token variable">$str</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="console-prompt" tabindex="-1"><a class="header-anchor" href="#console-prompt"><span>Console::prompt</span></a></h3>
<p>对用户的输入，提示并对其进行验证，通过验证后返回用户的输入字符串。以下是支持的验证输入的选项：</p>
<ul>
<li><code v-pre>required</code>: 是否是必填项</li>
<li><code v-pre>default</code>: 用户没有输入时的默认值</li>
<li><code v-pre>pattern</code>: 验证输入的正则表达式模式</li>
<li><code v-pre>validator</code>: 验证输入的回调函数，该函数必须接受两个参数:
<ul>
<li><code v-pre>input</code>: 要验证的用户输入内容</li>
<li><code v-pre>error</code>: 验证失败时通过引用传递的错误值。</li>
</ul>
</li>
</ul>
<p>示例代码如下：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// $options 验证输入的选项，默认为 []</span></span>
<span class="line"><span class="token variable">$options</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'required'</span> <span class="token operator">=></span> <span class="token constant boolean">true</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token comment">//'default' => 'yes',</span></span>
<span class="line">    <span class="token comment">//'pattern' => '/^[a-zA-Z0-9_]+$/',</span></span>
<span class="line">    <span class="token comment">/*'validator' => function($input, $error){</span>
<span class="line">        // ...</span>
<span class="line">        // return ture;</span>
<span class="line">    },*/</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$str</span> <span class="token operator">=</span> <span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">prompt</span><span class="token punctuation">(</span><span class="token variable">$text</span><span class="token punctuation">,</span> <span class="token variable">$options</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="console-confirm" tabindex="-1"><a class="header-anchor" href="#console-confirm"><span>Console::confirm</span></a></h3>
<p>询问用户输入 y 或 n 进行确认。典型的用法，如下所示：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// $default 如果没有进行选择，则返回该值，默认为 false</span></span>
<span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">confirm</span><span class="token punctuation">(</span><span class="token string double-quoted-string">"Are you sure?"</span><span class="token punctuation">,</span> <span class="token variable">$default</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">echo</span> <span class="token string double-quoted-string">"user typed yes\n"</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">echo</span> <span class="token string double-quoted-string">"user typed no\n"</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="console-select" tabindex="-1"><a class="header-anchor" href="#console-select"><span>Console::select</span></a></h3>
<p>给用户一个可供选择的选项。当输入 ‘?’ 时，就会显示出可供选择的选项及其解释。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// $prompt 提示消息</span></span>
<span class="line"><span class="token comment">// $options 要选择的选项的 键-值 数组。</span></span>
<span class="line"><span class="token comment">// 键是输入和使用的内容，值是通过帮助命令显示给终端用户的内容。</span></span>
<span class="line"><span class="token variable">$options</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'exit'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'exit cmd'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$str</span> <span class="token operator">=</span> <span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">select</span><span class="token punctuation">(</span><span class="token variable">$prompt</span><span class="token punctuation">,</span> <span class="token variable">$options</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="console-startprogress" tabindex="-1"><a class="header-anchor" href="#console-startprogress"><span>Console::startProgress</span></a></h3>
<p>开始在屏幕上显示进度条。这个进度条将由 <code v-pre>updateProgress()</code> 更新，并可能由 <code v-pre>endProgress()</code> 结束。下面的例子展示了进度条的简单用法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">startProgress</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token variable">$n</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token variable">$n</span> <span class="token operator">&lt;=</span> <span class="token number">1000</span><span class="token punctuation">;</span> <span class="token variable">$n</span><span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token function">usleep</span><span class="token punctuation">(</span><span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">updateProgress</span><span class="token punctuation">(</span><span class="token variable">$n</span><span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"><span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">endProgress</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Git clone like progress （只显示状态信息）:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">startProgress</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Counting objects: '</span><span class="token punctuation">,</span> <span class="token constant boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token variable">$n</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token variable">$n</span> <span class="token operator">&lt;=</span> <span class="token number">1000</span><span class="token punctuation">;</span> <span class="token variable">$n</span><span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token function">usleep</span><span class="token punctuation">(</span><span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">updateProgress</span><span class="token punctuation">(</span><span class="token variable">$n</span><span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"><span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">endProgress</span><span class="token punctuation">(</span><span class="token string double-quoted-string">"done."</span> <span class="token operator">.</span> <span class="token constant">PHP_EOL</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="console-updateprogress" tabindex="-1"><a class="header-anchor" href="#console-updateprogress"><span>Console::updateProgress</span></a></h3>
<p>更新由 <code v-pre>startProgress()</code> 启动的进度条。</p>
<h3 id="console-endprogress" tabindex="-1"><a class="header-anchor" href="#console-endprogress"><span>Console::endProgress</span></a></h3>
<p>结束由 <code v-pre>startProgress()</code> 启动的进度条。</p>
<h3 id="console-errorsummary" tabindex="-1"><a class="header-anchor" href="#console-errorsummary"><span>Console::errorSummary</span></a></h3>
<p>Yii 2.0.14 版本开始，提供此方法，用于生成验证错误的摘要，并返回该字符串。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// $models 要显示其验证错误的模型</span></span>
<span class="line"><span class="token comment">// $options 以 名称-值 对表示的标记选项。以下选项是特殊处理的：</span></span>
<span class="line"><span class="token comment">// showAllErrors：如果设置为 true，将显示每个属性的每个错误消息，否则只显示每个属性的第一个错误消息。默认为 `false`。</span></span>
<span class="line"><span class="token variable">$str</span> <span class="token operator">=</span> <span class="token class-name static-context">Console</span><span class="token operator">::</span><span class="token function">errorSummary</span><span class="token punctuation">(</span><span class="token variable">$models</span><span class="token punctuation">,</span> <span class="token variable">$options</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="filehelper" tabindex="-1"><a class="header-anchor" href="#filehelper"><span>FileHelper</span></a></h2>
<p>文件系统助手类。</p>
<h3 id="filehelper-mimemagicfile" tabindex="-1"><a class="header-anchor" href="#filehelper-mimemagicfile"><span>FileHelper::$mimeMagicFile</span></a></h3>
<p>包含 MIME 类型信息的 PHP 文件的路径(或别名)。</p>
<h3 id="filehelper-mimealiasesfile" tabindex="-1"><a class="header-anchor" href="#filehelper-mimealiasesfile"><span>FileHelper::$mimeAliasesFile</span></a></h3>
<p>Yii 2.0.14 版本开始提供此属性，包含 MIME 别名的 PHP 文件的路径(或别名)。</p>
<h3 id="filehelper-normalizepath" tabindex="-1"><a class="header-anchor" href="#filehelper-normalizepath"><span>FileHelper::normalizePath</span></a></h3>
<p>规范化 文件/目录 的路径。标准化做了以下工作：</p>
<ul>
<li>将所有目录分隔符转换为 <code v-pre>DIRECTORY_SEPARATOR</code> (如 “\a/b\c” 变成 “/a/b/c”)</li>
<li>删除尾随目录分隔符 (如 “/a/b/c/“ 变成 “/a/b/c”)</li>
<li>将多个连续斜杠变成一个单独的斜杠 (如 “/a///b/c” 变成 “/a/b/c”)</li>
<li>基于 “..” 和 “.” 的含义，删除它们 (如 “/a/./b/../c” 变成 “/a/c”)</li>
</ul>
<p>代码示例如下：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$path_normalize</span> <span class="token operator">=</span> <span class="token class-name static-context">FileHelper</span><span class="token operator">::</span><span class="token function">normalizePath</span><span class="token punctuation">(</span><span class="token variable">$path</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><blockquote>
<p>注意：对于已注册的 stream 包装器，将跳过连续的斜杠规则和 “..” / “.” 的转换。</p>
</blockquote>
<h3 id="filehelper-localize" tabindex="-1"><a class="header-anchor" href="#filehelper-localize"><span>FileHelper::localize</span></a></h3>
<p>返回指定文件的本地化版本。</p>
<p>这个搜索查询基于指定的语言编码。特别是，将在名称与语言代码相同的子目录下查找具有相同名称的文件。例如，给定文件 “path/to/view.php” 和 语言编码 “zh-CN”，那么，本地化文件将被作为 “path/to/zh-CN/view.php” 查找。如果没有找到该文件，它将尝试使用 “zh” 语言代码作为备选方案，如 “path/to/zh/view.php”。如果没有找到，则返回原始文件。</p>
<p>如果目标和源语言代码相同，则返回原始文件。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// $language 本地化目标语言。缺省时调用 `yii\base\Application::language` 的值</span></span>
<span class="line"><span class="token comment">// $sourceLanguage 文件的源语言。缺省时调用，`yii\base\Application::language` 的值</span></span>
<span class="line"><span class="token variable">$file_localized</span> <span class="token operator">=</span> <span class="token class-name static-context">FileHelper</span><span class="token operator">::</span><span class="token function">localize</span><span class="token punctuation">(</span><span class="token variable">$file</span><span class="token punctuation">,</span> <span class="token variable">$language</span><span class="token punctuation">,</span> <span class="token variable">$sourceLanguage</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="filehelper-getmimetype" tabindex="-1"><a class="header-anchor" href="#filehelper-getmimetype"><span>FileHelper::getMimeType</span></a></h3>
<p>确定指定文件的 MIME 类型。这个方法将首先尝试基于 PHP 函数 <a href="https://www.php.net/manual/zh/function.finfo-open.php" target="_blank" rel="noopener noreferrer">finfo_open</a> 确定 MIME 类型。如果没有安装 PHP <code v-pre>fileinfo</code> 扩展，当第三个参数 <code v-pre>$checkExtension</code> 为 true 时，会调用 <code v-pre>getMimeTypeByExtension()</code> 方法。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// $magicFile magic 数据库文件的名称（或别名），通常类似于  `/path/to/magic.mime`，默认为 null</span></span>
<span class="line"><span class="token comment">// 当安装了 `fileinfo` 扩展，这将作为第二个参数传递给 `finfo_open()`。如果 MIME 类型是通过 `getMimeTypeByExtension()` 确定的，并且这是 null，它将使用由 `mimeMagicFile` 指定的文件。</span></span>
<span class="line"><span class="token variable">$mine_type</span> <span class="token operator">=</span> <span class="token class-name static-context">FileHelper</span><span class="token operator">::</span><span class="token function">getMimeType</span><span class="token punctuation">(</span><span class="token variable">$file</span><span class="token punctuation">,</span> <span class="token variable">$magicFile</span><span class="token punctuation">,</span> <span class="token variable">$checkExtension</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="filehelper-getmimetypebyextension" tabindex="-1"><a class="header-anchor" href="#filehelper-getmimetypebyextension"><span>FileHelper::getMimeTypeByExtension</span></a></h3>
<p>根据指定文件的扩展名确定 MIME 类型。此方法将使用扩展名和 MIME 类型之间的本地映射。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// $magicFile 包含所有可用 MIME 类型信息的文件的路径(或别名)。如果缺省，将使用`$mimeMagicFile` 指定的文件。</span></span>
<span class="line"><span class="token variable">$mine_type</span> <span class="token operator">=</span> <span class="token class-name static-context">FileHelper</span><span class="token operator">::</span><span class="token function">getMimeTypeByExtension</span><span class="token punctuation">(</span><span class="token variable">$file</span><span class="token punctuation">,</span> <span class="token variable">$magicFile</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="filehelper-getextensionsbymimetype" tabindex="-1"><a class="header-anchor" href="#filehelper-getextensionsbymimetype"><span>FileHelper::getExtensionsByMimeType</span></a></h3>
<p>根据给定的 MIME 类型确定并返回扩展名数组。此方法将使用扩展名和 MIME 类型之间的本地映射。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// $magicFile 包含所有可用 MIME 类型信息的文件的路径(或别名)。如果缺省，将使用`$mimeMagicFile` 指定的文件。</span></span>
<span class="line"><span class="token variable">$mimeType</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'image/jpeg'</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$mine_type</span> <span class="token operator">=</span> <span class="token class-name static-context">FileHelper</span><span class="token operator">::</span><span class="token function">getExtensionsByMimeType</span><span class="token punctuation">(</span><span class="token variable">$mimeType</span><span class="token punctuation">,</span> <span class="token variable">$magicFile</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="filehelper-copydirectory" tabindex="-1"><a class="header-anchor" href="#filehelper-copydirectory"><span>FileHelper::copyDirectory</span></a></h3>
<p>将一个完整文件目录拷贝到另一个目录。文件和子目录也将被拷贝。</p>
<p>options for directory copy. Valid options are:</p>
<ul>
<li>
<p>dirMode: integer, the permission to be set for newly copied directories. Defaults to 0775.</p>
</li>
<li>
<p>fileMode: integer, the permission to be set for newly copied files. Defaults to the current environment setting.</p>
</li>
<li>
<p>filter: callback, a PHP callback that is called for each directory or file. The signature of the callback should be: <code v-pre>function ($path)</code>, where <code v-pre>$path</code> refers the full path to be filtered. The callback can return one of the following values:</p>
<ul>
<li>true: the directory or file will be copied (the “only” and “except” options will be ignored)</li>
<li>false: the directory or file will NOT be copied (the “only” and “except” options will be ignored)</li>
<li>null: the “only” and “except” options will determine whether the directory or file should be copied</li>
</ul>
</li>
<li>
<p>only: array, list of patterns that the file paths should match if they want to be copied.<br>
A path matches a pattern if it contains the pattern string at its end.<br>
For example, ‘.php’ matches all file paths ending with ‘.php’.<br>
Note, the ‘/‘ characters in a pattern matches both ‘/‘ and ‘' in the paths.<br>
If a file path matches a pattern in both “only” and “except”, it will NOT be copied.</p>
</li>
<li>
<p>except: array, list of patterns that the files or directories should match if they want to be excluded from being copied.<br>
A path matches a pattern if it contains the pattern string at its end.<br>
Patterns ending with ‘/‘ apply to directory paths only, and patterns not ending with ‘/‘<br>
apply to file paths only. For example, ‘/a/b’ matches all file paths ending with ‘/a/b’;<br>
and ‘.svn/‘ matches directory paths ending with ‘.svn’. Note, the ‘/‘ characters in a pattern matches<br>
both ‘/‘ and ‘' in the paths.</p>
</li>
<li>
<p>caseSensitive: boolean, whether patterns specified at “only” or “except” should be case sensitive. Defaults to true.</p>
</li>
<li>
<p>recursive: boolean, whether the files under the subdirectories should also be copied. Defaults to true.</p>
</li>
<li>
<p>beforeCopy: callback, a PHP callback that is called before copying each sub-directory or file.<br>
If the callback returns false, the copy operation for the sub-directory or file will be cancelled.<br>
The signature of the callback should be: <code v-pre>function ($from, $to)</code>, where <code v-pre>$from</code> is the sub-directory or<br>
file to be copied from, while <code v-pre>$to</code> is the copy target.</p>
</li>
<li>
<p>afterCopy: callback, a PHP callback that is called after each sub-directory or file is successfully copied.<br>
The signature of the callback should be: <code v-pre>function ($from, $to)</code>, where <code v-pre>$from</code> is the sub-directory or<br>
file copied from, while <code v-pre>$to</code> is the copy target.</p>
</li>
<li>
<p>copyEmptyDirectories: boolean, whether to copy empty directories. Set this to false to avoid creating directories<br>
that do not contain files. This affects directories that do not contain files initially as well as directories that<br>
do not contain files at the target destination because files have been filtered via <code v-pre>only</code> or <code v-pre>except</code>.<br>
Defaults to true. This option is available since version 2.0.12. Before 2.0.12 empty directories are always copied.</p>
</li>
</ul>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">FileHelper</span><span class="token operator">::</span><span class="token function">copyDirectory</span><span class="token punctuation">(</span><span class="token variable">$src</span><span class="token punctuation">,</span> <span class="token variable">$dst</span><span class="token punctuation">,</span> <span class="token variable">$options</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="filehelper-removedirectory" tabindex="-1"><a class="header-anchor" href="#filehelper-removedirectory"><span>FileHelper::removeDirectory</span></a></h3>
<p>Removes a directory (and all its content) recursively.<br>
$options options for directory remove. Valid options are:</p>
<ul>
<li>traverseSymlinks: boolean, whether symlinks to the directories should be traversed too.<br>
Defaults to <code v-pre>false</code>, meaning the content of the symlinked directory would not be deleted.<br>
Only symlink would be removed in that default case.</li>
</ul>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">FileHelper</span><span class="token operator">::</span><span class="token function">removeDirectory</span><span class="token punctuation">(</span><span class="token variable">$dir</span><span class="token punctuation">,</span> <span class="token variable">$options</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="filehelper-unlink" tabindex="-1"><a class="header-anchor" href="#filehelper-unlink"><span>FileHelper::unlink</span></a></h3>
<p>Yii 2.0.14 版本开始提供此方法，以跨平台的方式删除文件或符号链接，并返回布尔类型的结果。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$res</span> <span class="token operator">=</span> <span class="token class-name static-context">FileHelper</span><span class="token operator">::</span><span class="token function">unlink</span><span class="token punctuation">(</span><span class="token variable">$path</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="filehelper-findfiles" tabindex="-1"><a class="header-anchor" href="#filehelper-findfiles"><span>FileHelper::findFiles</span></a></h3>
<p>Returns the files found under the specified directory and subdirectories. files found under the directory, in no particular order. Ordering depends on the files system used. 第二个参数 <code v-pre>$options</code> 是文件搜索选项。Valid options are:</p>
<ul>
<li><code v-pre>filter</code>: callback, a PHP callback that is called for each directory or file.<br>
The signature of the callback should be: <code v-pre>function ($path)</code>, where <code v-pre>$path</code> refers the full path to be filtered.<br>
The callback can return one of the following values:
<ul>
<li><code v-pre>true</code>: the directory or file will be returned (the <code v-pre>only</code> and <code v-pre>except</code> options will be ignored)</li>
<li><code v-pre>false</code>: the directory or file will NOT be returned (the <code v-pre>only</code> and <code v-pre>except</code> options will be ignored)</li>
<li><code v-pre>null</code>: the <code v-pre>only</code> and <code v-pre>except</code> options will determine whether the directory or file should be returned</li>
</ul>
</li>
<li><code v-pre>except</code>: array, list of patterns excluding from the results matching file or directory paths.<br>
Patterns ending with slash (‘/‘) apply to directory paths only, and patterns not ending with ‘/‘<br>
apply to file paths only. For example, ‘/a/b’ matches all file paths ending with ‘/a/b’;<br>
and <code v-pre>.svn/</code> matches directory paths ending with <code v-pre>.svn</code>.<br>
If the pattern does not contain a slash (<code v-pre>/</code>), it is treated as a shell glob pattern<br>
and checked for a match against the pathname relative to <code v-pre>$dir</code>.<br>
Otherwise, the pattern is treated as a shell glob suitable for consumption by <code v-pre>fnmatch(3)</code><br>
with the <code v-pre>FNM_PATHNAME</code> flag: wildcards in the pattern will not match a <code v-pre>/</code> in the pathname.<br>
For example, <code v-pre>views/*.php</code> matches <code v-pre>views/index.php</code> but not <code v-pre>views/controller/index.php</code>.<br>
A leading slash matches the beginning of the pathname. For example, <code v-pre>/*.php</code> matches <code v-pre>index.php</code> but not <code v-pre>views/start/index.php</code>.<br>
An optional prefix <code v-pre>!</code> which negates the pattern; any matching file excluded by a previous pattern will become included again.<br>
If a negated pattern matches, this will override lower precedence patterns sources. Put a backslash (<code v-pre>\</code>) in front of the first <code v-pre>!</code><br>
for patterns that begin with a literal <code v-pre>!</code>, for example, <code v-pre>\!important!.txt</code>.<br>
Note, the ‘/‘ characters in a pattern matches both ‘/‘ and ‘' in the paths.</li>
<li><code v-pre>only</code>: array, list of patterns that the file paths should match if they are to be returned. Directory paths<br>
are not checked against them. Same pattern matching rules as in the <code v-pre>except</code> option are used.<br>
If a file path matches a pattern in both <code v-pre>only</code> and <code v-pre>except</code>, it will NOT be returned.</li>
<li><code v-pre>caseSensitive</code>: boolean, whether patterns specified at <code v-pre>only</code> or <code v-pre>except</code> should be case sensitive. Defaults to <code v-pre>true</code>.</li>
<li><code v-pre>recursive</code>: boolean, whether the files under the subdirectories should also be looked for. Defaults to <code v-pre>true</code>.</li>
</ul>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$file_arr</span> <span class="token operator">=</span> <span class="token class-name static-context">FileHelper</span><span class="token operator">::</span><span class="token function">findFiles</span><span class="token punctuation">(</span><span class="token variable">$dir</span><span class="token punctuation">,</span> <span class="token variable">$options</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="filehelper-finddirectories" tabindex="-1"><a class="header-anchor" href="#filehelper-finddirectories"><span>FileHelper::findDirectories</span></a></h3>
<p>Yii 2.0.14 版本开始提供此方法，Returns the directories found under the specified directory and subdirectories. directories found under the directory, in no particular order. Ordering depends on the files system used. 第二个参数 <code v-pre>$options</code> 是目录搜索选项. Valid options are:</p>
<ul>
<li><code v-pre>filter</code>: callback, a PHP callback that is called for each directory or file.<br>
The signature of the callback should be: <code v-pre>function (string $path): bool</code>, where <code v-pre>$path</code> refers the full path to be filtered. The callback can return one of the following values:
<ul>
<li><code v-pre>true</code>: the directory will be returned</li>
<li><code v-pre>false</code>: the directory will NOT be returned</li>
</ul>
</li>
<li><code v-pre>recursive</code>: boolean, whether the files under the subdirectories should also be looked for. Defaults to <code v-pre>true</code>.</li>
</ul>
<p>See [[findFiles()]] for more options.</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$file_arr</span> <span class="token operator">=</span> <span class="token class-name static-context">FileHelper</span><span class="token operator">::</span><span class="token function">findDirectories</span><span class="token punctuation">(</span><span class="token variable">$dir</span><span class="token punctuation">,</span> <span class="token variable">$options</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="filehelper-filterpath" tabindex="-1"><a class="header-anchor" href="#filehelper-filterpath"><span>FileHelper::filterPath</span></a></h3>
<p>检查给定的文件路径是否满足过滤选项。第二个参数 <code v-pre>$options</code> 是目录搜索选项，See [[findFiles()]] for explanations of the supported options.</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$res</span> <span class="token operator">=</span> <span class="token class-name static-context">FileHelper</span><span class="token operator">::</span><span class="token function">filterPath</span><span class="token punctuation">(</span><span class="token variable">$path</span><span class="token punctuation">,</span> <span class="token variable">$options</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="filehelper-createdirectory" tabindex="-1"><a class="header-anchor" href="#filehelper-createdirectory"><span>FileHelper::createDirectory</span></a></h3>
<p>创建一个新目录，成功返回 true。<br>
This method is similar to the PHP <code v-pre>mkdir()</code> function except that it uses <code v-pre>chmod()</code> to set the permission of the created directory in order to avoid the impact of the <code v-pre>umask</code> setting.</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$res</span> <span class="token operator">=</span> <span class="token class-name static-context">FileHelper</span><span class="token operator">::</span><span class="token function">createDirectory</span><span class="token punctuation">(</span><span class="token variable">$path</span><span class="token punctuation">,</span> <span class="token variable">$mode</span><span class="token punctuation">,</span> <span class="token variable">$recursive</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="filehelper-changeownership" tabindex="-1"><a class="header-anchor" href="#filehelper-changeownership"><span>FileHelper::changeOwnership</span></a></h3>
<p>Yii 2.0.43 版本开始提供此方法，Changes the Unix user and/or group ownership of a file or directory, and optionally the mode.<br>
第二个参数 <code v-pre>$ownership</code> 是 the user and/or group ownership for the file or directory. When $ownership is a string, the format is ‘user:group’ where both are optional. E.g. ‘user’ or ‘user:’ will only change the user, ‘:group’ will only change the group, ‘user:group’ will change both. When $owners is an index array the format is [0 =&gt; user, 1 =&gt; group], e.g. <code v-pre>[$myUser, $myGroup]</code>. It is also possible to pass an associative array, e.g. [‘user’ =&gt; $myUser, ‘group’ =&gt; $myGroup]. In case $owners is an integer it will be used as user id. If <code v-pre>null</code>, an empty array or an empty string is passed, the ownership will not be changed.</p>
<p>第三个参数 <code v-pre>$mode</code> 是 the permission to be set for the file or directory. If <code v-pre>null</code> is passed, the mode will not be changed.</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">FileHelper</span><span class="token operator">::</span><span class="token function">changeOwnership</span><span class="token punctuation">(</span><span class="token variable">$path</span><span class="token punctuation">,</span> <span class="token variable">$ownership</span><span class="token punctuation">,</span> <span class="token variable">$mode</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>Note: This function will not work on remote files as the file to be examined must be accessible via the server’s filesystem.<br>
Note: On Windows, this function fails silently when applied on a regular file.</p>
<h2 id="formatconverter" tabindex="-1"><a class="header-anchor" href="#formatconverter"><span>FormatConverter</span></a></h2>
<p>FormatConverter provides functionality to convert between different formatting pattern formats. It provides functions to convert date format patterns between different conventions.</p>
<h3 id="formatconverter-phpfallbackdatepatterns" tabindex="-1"><a class="header-anchor" href="#formatconverter-phpfallbackdatepatterns"><span>FormatConverter::$phpFallbackDatePatterns</span></a></h3>
<p>the php fallback definition to use for the ICU short patterns <code v-pre>short</code>, <code v-pre>medium</code>, <code v-pre>long</code> and <code v-pre>full</code>. This is used as fallback when the intl extension is not installed.</p>
<h3 id="formatconverter-juifallbackdatepatterns" tabindex="-1"><a class="header-anchor" href="#formatconverter-juifallbackdatepatterns"><span>FormatConverter::$juiFallbackDatePatterns</span></a></h3>
<p>the jQuery UI fallback definition to use for the ICU short patterns <code v-pre>short</code>, <code v-pre>medium</code>, <code v-pre>long</code> and <code v-pre>full</code>. This is used as fallback when the intl extension is not installed.</p>
<h3 id="formatconverter-convertdateicutophp" tabindex="-1"><a class="header-anchor" href="#formatconverter-convertdateicutophp"><span>FormatConverter::convertDateIcuToPhp</span></a></h3>
<p>Converts a date format pattern from [ICU format][] to <a href="https://www.php.net/manual/en/function.date.php" target="_blank" rel="noopener noreferrer">php date() function format</a>.<br>
The conversion is limited to date patterns that do not use escaped characters.<br>
Patterns like <code v-pre>d 'of' MMMM yyyy</code> which will result in a date like <code v-pre>1 of December 2014</code> may not be converted correctly because of the use of escaped characters.<br>
Pattern constructs that are not supported by the PHP format will be removed.<br>
<a href="https://www.php.net/manual/en/function.date.php" target="_blank" rel="noopener noreferrer">php date() function format</a>: <a href="https://www.php.net/manual/en/function.date.php" target="_blank" rel="noopener noreferrer">www.php.net/manual/en/function.dat...</a><br>
[ICU format]: <a href="http://userguide.icu-project.org/formatparse/datetime#TOC-Date-Time-Format-Syntax" target="_blank" rel="noopener noreferrer">userguide.icu-project.org/formatpar...</a></p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// $pattern date format pattern in ICU format.</span></span>
<span class="line"><span class="token comment">// $type 'date', 'time', or 'datetime'.</span></span>
<span class="line"><span class="token comment">// $locale the locale to use for converting ICU short patterns `short`, `medium`, `long` and `full`. If not given, `Yii::$app->language` will be used.</span></span>
<span class="line"><span class="token variable">$pattern_format</span> <span class="token operator">=</span> <span class="token class-name static-context">FormatConverter</span><span class="token operator">::</span><span class="token function">convertDateIcuToPhp</span><span class="token punctuation">(</span><span class="token variable">$pattern</span><span class="token punctuation">,</span> <span class="token variable">$type</span><span class="token punctuation">,</span> <span class="token variable">$locale</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="formatconverter-convertdatephptoicu" tabindex="-1"><a class="header-anchor" href="#formatconverter-convertdatephptoicu"><span>FormatConverter::convertDatePhpToIcu</span></a></h3>
<p>Converts a date format pattern from <a href="https://www.php.net/manual/en/function.date.php" target="_blank" rel="noopener noreferrer">php date() function format</a> to [ICU format][].<br>
Pattern constructs that are not supported by the ICU format will be removed.<br>
<a href="https://www.php.net/manual/en/function.date.php" target="_blank" rel="noopener noreferrer">php date() function format</a>: <a href="https://www.php.net/manual/en/function.date.php" target="_blank" rel="noopener noreferrer">www.php.net/manual/en/function.dat...</a><br>
[ICU format]: <a href="http://userguide.icu-project.org/formatparse/datetime#TOC-Date-Time-Format-Syntax" target="_blank" rel="noopener noreferrer">userguide.icu-project.org/formatpar...</a><br>
Since 2.0.13 it handles escaped characters correctly.</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// $pattern date format pattern in php date()-function format.</span></span>
<span class="line"><span class="token variable">$pattern_format</span> <span class="token operator">=</span> <span class="token class-name static-context">FormatConverter</span><span class="token operator">::</span><span class="token function">convertDatePhpToIcu</span><span class="token punctuation">(</span><span class="token variable">$pattern</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="formatconverter-convertdateicutojui" tabindex="-1"><a class="header-anchor" href="#formatconverter-convertdateicutojui"><span>FormatConverter::convertDateIcuToJui</span></a></h3>
<p>Converts a date format pattern from [ICU format][] to <a href="http://api.jqueryui.com/datepicker/#utility-formatDate" target="_blank" rel="noopener noreferrer">jQuery UI date format</a>.</p>
<p>Pattern constructs that are not supported by the jQuery UI format will be removed.<br>
<a href="http://api.jqueryui.com/datepicker/#utility-formatDate" target="_blank" rel="noopener noreferrer">jQuery UI date format</a>: <a href="http://api.jqueryui.com/datepicker/#utility-formatDate" target="_blank" rel="noopener noreferrer">api.jqueryui.com/datepicker/#utilit...</a><br>
[ICU format]: <a href="http://userguide.icu-project.org/formatparse/datetime#TOC-Date-Time-Format-Syntax" target="_blank" rel="noopener noreferrer">userguide.icu-project.org/formatpar...</a></p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// $pattern date format pattern in ICU format.</span></span>
<span class="line"><span class="token comment">// $type 'date', 'time', or 'datetime'.</span></span>
<span class="line"><span class="token comment">// $locale the locale to use for converting ICU short patterns `short`, `medium`, `long` and `full`. If not given, `Yii::$app->language` will be used.</span></span>
<span class="line"><span class="token variable">$pattern_format</span> <span class="token operator">=</span> <span class="token class-name static-context">FormatConverter</span><span class="token operator">::</span><span class="token function">convertDateIcuToJui</span><span class="token punctuation">(</span><span class="token variable">$pattern</span><span class="token punctuation">,</span> <span class="token variable">$type</span><span class="token punctuation">,</span> <span class="token variable">$locale</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="formatconverter-convertdatephptojui" tabindex="-1"><a class="header-anchor" href="#formatconverter-convertdatephptojui"><span>FormatConverter::convertDatePhpToJui</span></a></h3>
<p>Converts a date format pattern from <a href="https://www.php.net/manual/en/function.date.php" target="_blank" rel="noopener noreferrer">php date() function format</a> to <a href="http://api.jqueryui.com/datepicker/#utility-formatDate" target="_blank" rel="noopener noreferrer">jQuery UI date format</a>.<br>
The conversion is limited to date patterns that do not use escaped characters.<br>
Patterns like <code v-pre>jS \o\f F Y</code> which will result in a date like <code v-pre>1st of December 2014</code> may not be converted correctly because of the use of escaped characters.</p>
<p>Pattern constructs that are not supported by the jQuery UI format will be removed.</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// $pattern date format pattern in php date()-function format.</span></span>
<span class="line"><span class="token variable">$pattern_format</span> <span class="token operator">=</span> <span class="token class-name static-context">FormatConverter</span><span class="token operator">::</span><span class="token function">convertDatePhpToJui</span><span class="token punctuation">(</span><span class="token variable">$pattern</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="html" tabindex="-1"><a class="header-anchor" href="#html"><span>Html</span></a></h2>
<p>Html provides a set of static methods for generating commonly used HTML tags.</p>
<p>Nearly all of the methods in this class allow setting additional html attributes for the html tags they generate. You can specify, for example, <code v-pre>class</code>, <code v-pre>style</code> or <code v-pre>id</code> for an html element using the <code v-pre>$options</code> parameter. See the documentation of the [[tag()]] method for more details.</p>
<p>For more details and usage information on Html, see the guide article on html helpers.</p>
<h2 id="htmlpurifier" tabindex="-1"><a class="header-anchor" href="#htmlpurifier"><span>HtmlPurifier</span></a></h2>
<p>HtmlPurifier provides an ability to clean up HTML from any harmful code.</p>
<p>Basic usage is the following:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">echo</span> <span class="token class-name static-context">HtmlPurifier</span><span class="token operator">::</span><span class="token function">process</span><span class="token punctuation">(</span><span class="token variable">$html</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>If you want to configure it:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">echo</span> <span class="token class-name static-context">HtmlPurifier</span><span class="token operator">::</span><span class="token function">process</span><span class="token punctuation">(</span><span class="token variable">$html</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'Attr.EnableID'</span> <span class="token operator">=></span> <span class="token constant boolean">true</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>For more details please refer to <a href="http://htmlpurifier.org/" target="_blank" rel="noopener noreferrer">HTMLPurifier documentation</a>.</p>
<h2 id="image" tabindex="-1"><a class="header-anchor" href="#image"><span>Image</span></a></h2>
<p>Image implements most commonly used image manipulation functions using the <a href="http://imagine.readthedocs.org/" target="_blank" rel="noopener noreferrer">Imagine library</a>.</p>
<p>Example of use:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// generate a thumbnail image</span></span>
<span class="line"><span class="token class-name static-context">Image</span><span class="token operator">::</span><span class="token function">thumbnail</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'@webroot/img/test-image.jpg'</span><span class="token punctuation">,</span> <span class="token number">120</span><span class="token punctuation">,</span> <span class="token number">120</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">save</span><span class="token punctuation">(</span><span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token function">getAlias</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'@runtime/thumb-test-image.jpg'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'quality'</span> <span class="token operator">=></span> <span class="token number">50</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="inflector" tabindex="-1"><a class="header-anchor" href="#inflector"><span>Inflector</span></a></h2>
<h2 id="iphelper" tabindex="-1"><a class="header-anchor" href="#iphelper"><span>IpHelper</span></a></h2>
<h2 id="json" tabindex="-1"><a class="header-anchor" href="#json"><span>Json</span></a></h2>
<h2 id="markdown" tabindex="-1"><a class="header-anchor" href="#markdown"><span>Markdown</span></a></h2>
<h2 id="stringhelper" tabindex="-1"><a class="header-anchor" href="#stringhelper"><span>StringHelper</span></a></h2>
<h2 id="url" tabindex="-1"><a class="header-anchor" href="#url"><span>Url</span></a></h2>
<h2 id="vardumper" tabindex="-1"><a class="header-anchor" href="#vardumper"><span>VarDumper</span></a></h2>
<h2 id="自定义助手类" tabindex="-1"><a class="header-anchor" href="#自定义助手类"><span>自定义助手类</span></a></h2>
<p>如果您想要自定义一个核心助手类（如 <code v-pre>yii\helpers\ArrayHelper</code>），你应该从对应的基类（如 <code v-pre>yii\helpers\BaseArrayHelper</code>）中创建一个新的类，将您的类命名为与相应的具体类相同的名称（如 <code v-pre>yii\helpers\ArrayHelper</code>），并包括其名称空间。然后这个类将被设置来替换框架的原始实现。</p>
<p>下面的列子展示了，如何自定义 <code v-pre>yii\helpers\ArrayHelper::merge()</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">yii<span class="token punctuation">\</span>helpers</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">ArrayHelper</span> <span class="token keyword">extends</span> <span class="token class-name">BaseArrayHelper</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">function</span> <span class="token function-definition function">merge</span><span class="token punctuation">(</span><span class="token variable">$a</span><span class="token punctuation">,</span> <span class="token variable">$b</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token comment">// your custom implementation</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>将您的类保存到名为 <code v-pre>ArrayHelper.php</code> 的文件中，该文件可以在任何目录下，例如：<code v-pre>@app/components</code>。</p>
<p>下一步，在你的应用入口文件中，在包含 <code v-pre>yii.php</code> 文件之后，添加以下代码行，告诉 Yii 类自动加载器 加载您的自定义类，而不是来自框架的原始助手类：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$classMap</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'yii\helpers\ArrayHelper'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'@app/components/ArrayHelper.php'</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><blockquote>
<p>注意：只有当你想改变助手类的一个现有函数的行为时，自定义助手类才有用。如果您想在您的应用程序中添加额外的功能，您最好为它创建一个单独的助手。</p>
</blockquote>
<blockquote>
<p>💖喜欢本文档的，欢迎点赞、收藏、留言或转发，谢谢支持！<br>
作者邮箱：zhuzixian520@126.com，github地址：<a href="https://github.com/zhuzixian520" target="_blank" rel="noopener noreferrer">github.com/zhuzixian520</a></p>
</blockquote>
</div></template>


